package com.Adapters;


import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import com.Adapters.Items.ItemComment;
import com.Adapters.Items.ItemPost;
import com.Data.Comment;
import com.Managers.FragmentManagerClass;


public class CommentAdapter extends ArrayAdapter<Comment> {

    private final ArrayList<Integer> idList;

    private ArrayList<ItemPost> rows;

    public CommentAdapter(Context context, int resource,
                int textViewResourceId) {

        super(context, resource, textViewResourceId);
        this.rows = new ArrayList<ItemPost>();
        this.idList = new ArrayList<Integer>();

    }

    public void add(Comment comment, int idOwner) {

        rows.add(new ItemComment(idOwner, LayoutInflater.from(getContext()), comment, this));
        idList.add(comment.getIDComment());
        if (FragmentManagerClass.aComment != null)
            FragmentManagerClass.aComment.updatePictureCounter(rows.size());
    }

    public void clearAnimations() {

        if (rows.size() > 0) {
            rows.get(0).clearAnimatonList();
        }
    }

    public boolean contains(Comment comment) {

        return idList.contains(comment.getIDComment());
    }

    @Override
    public int getCount() {

        return rows.size();
    }

    @Override
    public Comment getItem(int position) {

        return ((ItemComment) rows.get(position)).getComment();
    }

    public ItemComment getItemComment(int position) {

        return ((ItemComment) rows.get(position));
    }

    @Override
    public long getItemId(int position) {

        return ((ItemComment) rows.get(position)).getComment().getIDComment();
    }

    @Override
    public int getItemViewType(int position) {

        return rows.get(position).getViewType();
    }

    @Override
    public View getView(int position, View v, ViewGroup parent) {

        return rows.get(position).getView(v, position);
    }

    @Override
    public int getViewTypeCount() {

        return 1;
    }

    @Override
    public void remove(Comment object) {

        for (int i = 0; i < rows.size(); i++) {
            ItemComment ic = (ItemComment) rows.get(i);
            Comment com = ic.getComment();
            if (object.getIDComment() == com.getIDComment()) {
                rows.remove(i);
                // MEANS YOURE DELETING A COMMENT
                if (FragmentManagerClass.aComment != null)
                    FragmentManagerClass.aComment.updatePictureCounter(PostAdapter.COMMENT_LESS);
                notifyDataSetChanged();
            }
        }
        if (getCount() == 0) {
            if (FragmentManagerClass.aComment != null) {
                FragmentManagerClass.aComment.setEmpty(true);
                FragmentManagerClass.aComment.refreshComments(0);

            }

        }

    }
}
